package com.ellricky.market.ui

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.adapter.TaskAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerEntityListener
import com.ellricky.baselib.dialog.picker.EPickerStrListener
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.new.ClubTask
import com.ellricky.baselib.entity.new.MyClueData
import com.ellricky.baselib.entity.new.MyClueList
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewECallback
import com.ellricky.baselib.widget.calendar.DateUtils
import com.ellricky.market.R
import kotlinx.android.synthetic.main.market_fragment_clue_pool.*
import kotlinx.android.synthetic.main.market_fragment_clue_task.*
import kotlinx.android.synthetic.main.market_fragment_clue_task.rvView
import java.util.*

class MarketClueTaskFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var ePickerUtil: EPickerUtil? = null
    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<ClubTask> = ArrayList<ClubTask>()
    private var refresh = false

    private val taskAdapter by lazy {
        TaskAdapter()
    }

    companion object {
        fun newInstance(): MarketClueTaskFragment {
            val fragment = MarketClueTaskFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.market_fragment_clue_task
    }

    override fun initUIView(p0: View?) {
        ePickerUtil = EPickerUtil(mContext)
        setRefreshListener()
        initParamMap()
        loadDataOnInit()
        initOnclick()
    }

    private fun initOnclick() {
        rl_time.setOnClickListener {
            ePickerUtil?.showStartDatePicker(""
            ) { content, picPos, pickType ->
                val list = content.split("-")
                val year = list[0]
                var month = list[1]
                if (month.startsWith("0")) {
                    month = month.replace("0", "")
                }
                paramMap["year"] = year
                paramMap["month"] = month
                loadDataOnInit()
            }
        }
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    override fun setAdapter(rv: RecyclerView) {
        val adapter = taskAdapter
        adapter.setOnItemClickListener(this)
        rv.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()

        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun initParamMap() {
        paramMap["action"] = "totalPlanList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["year"] = getCurrentYear(System.currentTimeMillis())
        paramMap["month"] = getCurrentMonth(System.currentTimeMillis())
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewECallback<ClubTask>(mContext) {
            override fun onSuccess(data: ClubTask, msg: String?) {
                dataSource = arrayListOf(data)
                rvView.onLoadFinish(arrayListOf(data), refresh)
            }
        }
    }

    private fun setRefreshListener() {
        rvView.init(this)
    }

    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) taskAdapter.refresh(dataSource)
        else taskAdapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }

    private fun getCurrentMonth(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        val month = cal[Calendar.MONTH] + 1
        return month.toString()
    }

    private fun getCurrentYear(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        return cal[Calendar.YEAR].toString()
    }
}